Page({
    data: {
        phone: '', //供应商电话
        loading: false,
        nickname: '', //获取到的微信昵称
        nicknameErr: false,
        face: '', //获取到的微信头像
        code: '', //输入的验证码
        codeErr: false,
        enableSendCode: true, //允许发送验证码
        text: '发送',
        buttonLoading: false,
    },
    onLoad: function(options) {
        this.setData({ phone: options.phone })
    },
    /**点击绑定 */
    bind() {
        if (this.data.loading) return
        this.data.loading = true
        let that = this
        wx.getUserProfile({
            desc: '展示个人头像、昵称', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
            success: ({ userInfo }) => {
                that.setData({
                    nickname: userInfo.nickName,
                    face: userInfo.avatarUrl,
                    loading: false,
                    nicknameErr: false
                })
            },
            fail: () => {
                that.data.loading = false
            }
        })
    },
    /**获取验证码 */
    getCode() {
        if (!this.data.enableSendCode) return
        const { phone } = this.data
        if (phone === '') return wx.showToast({
            title: '手机号不存在',
            icon: 'none'
        })
        this.data.enableSendCode = false
        this.request({
            url: '/supplier/sms',
            data: { mobile: phone }
        }, () => {
            wx.showToast({
                title: '短信验证码已成功发送至您的手机，请注意查看',
                icon: 'none',
                duration: 3000
            })
            this.setContDown()
        }, () => {
            this.data.enableSendCode = true
        })
    },
    //设置倒计时
    setContDown() {
        this.setData({ text: '60s' })
        let time = 60
        const timeId = setInterval(() => {
            time--
            if (time <= 0) {
                this.data.enableSendCode = true
                this.setData({ text: '发送' })
                return clearInterval(timeId)
            }
            this.setData({ text: time + 's' })
        }, 1000)
    },
    /**确认 */
    confirm() {
        const { nickname, face, code } = this.data
        if (nickname === '' || face === '') return this.setData({ nicknameErr: true })
        if (code === '') return this.setData({ codeErr: true })
        this.setData({ buttonLoading: true })
        let that = this
        wx.login({ //获取code 
            withCredentials: true,
            timeout: 10000,
            success(res) {
                that.request({
                    url: '/supplier/supplier/wxBind',
                    method: 'POST',
                    data: {
                        sms_code: code,
                        code: res.code,
                        nickname,
                        face
                    }
                }, () => {
                    wx.showToast({
                        title: '绑定成功',
                        mask: true
                    })
                    that.delay(() => {
                        wx.navigateBack()
                    })
                }, () => {
                    that.setData({ buttonLoading: false })
                    that.data.loading = false
                })
            }
        })
    },
})